#ifndef __SPWM_H__
#define __SPWM_H__

#include "plat/nonecopyable.h"
#include "soc/soc_timer.h"
#include "soc/soc_gpio.h"
#include "softpwmitem.h"
#include "lib/fastTrig.h"

//spwm 调制表  
//典型值 [调制波 20khz 载波50HZ]
#define SPWM_MODULATE_SIN_MAX 400

/// @brief spwm调制
class SPwm : public NoneCopyable
{
public:
    enum class Direction
    {
        Positive = 0,
        Negative
    };

public:
    explicit SPwm();
    explicit SPwm(SOC_Timer *timer, int ch, float ma);
    virtual ~SPwm();

public:
    void Config(SOC_Timer *timer, int ch, float ma);
    void Start();
    void Stop();
    void SetPeriod(int period);
    void SetDuty(float duty);
    void SetCompare(int val);

    //0-360°
    void SetAngle(float angle);
    void SetIndex(int index);

    //计算完参数后，一次性更新，防止计算延时造成三相偏差
    void UpdateToPwm();
    int GetLastCompare();

public:
    static void InitTable(int maxPeriod);

private:
    SOC_Timer *m_timer = nullptr;
    int m_halfPeriod = 0;  //优化速度
    int m_ch = -1;
    float m_mA = 0.7;//调制度
    int m_lastCp = 0;
};

#endif // __SPWM_H__
